package com.bjyhl.ui.recycler;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bjyhl.core.bean.ItemEntity;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.bjyhl.ui.R;
import com.bjyhl.ui.banner.BannerCreator;

import java.util.ArrayList;
import java.util.List;

public class MultipleRecyclerAdapter extends
        BaseMultiItemQuickAdapter<MultipleItemEntity, MultipleViewHolder>
        implements
        BaseQuickAdapter.SpanSizeLookup,
        OnItemClickListener {

    public final static int SPAN_SIZE_TOTAL = 60;

    protected MultipleRecyclerAdapter(List<MultipleItemEntity> data) {
        super(data);
        init();
    }



    //    @Override
//    public MultipleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        return super.onCreateViewHolder(parent, viewType);
//    }



    //    @Override
//    public void onBindViewHolder(MultipleViewHolder holder, int position) {
//        super.onBindViewHolder(holder, position);
//    }

//    @Override
//    public void onBindViewHolder(@NonNull MultipleViewHolder holder, int position, @NonNull List<Object> payloads) {
//        super.onBindViewHolder(holder, position, payloads);
//    }

    //确保初始化一次Banner,防止重复Item加载
    private boolean mIsInitBanner = false;
    //设置图片加载策略
    private static final RequestOptions REQUEST_OPTIONS =
            new RequestOptions()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .dontAnimate();

    @Override
    protected void convert(MultipleViewHolder holder, MultipleItemEntity item) {
        final String title;
        final String text;
        final String tag;
        final String imageUrl;
        final String date;
        final String author;
        final ArrayList<String> bannerImages;
        final ArrayList<ItemEntity> itemEntities;
        final ItemEntity itemEntity;
        final String code;
        switch (holder.getItemViewType()) {

            case ItemType.NEWS:
                title = item.getField(MultipleFields.TITLE);
                text = item.getField(MultipleFields.TEXT);
                date = item.getField(MultipleFields.DATE);
                author = item.getField(MultipleFields.TAG);
                holder.setText(R.id.tv_title, title);
                holder.setText(R.id.tv_summary, text);
                holder.setText(R.id.tv_author, author);
                holder.setTag(R.id.tv_title, title+"@2@");
                break;
            case ItemType.NEWS_WITH_PIC:
                title = item.getField(MultipleFields.TITLE);
                text = item.getField(MultipleFields.TEXT);
                date = item.getField(MultipleFields.DATE);
                author = item.getField(MultipleFields.TAG);
                imageUrl = item.getField(MultipleFields.IMAGE_URL);

                holder.setText(R.id.tv_title, title);
                holder.setText(R.id.tv_summary, text);
                holder.setText(R.id.tv_author, author);
                Glide.with(mContext)
                        .load(imageUrl)
                        .apply(REQUEST_OPTIONS)
                        .into((ImageView) holder.getView(R.id.iv_image));
                break;

            case ItemType.TEXT:
                text = item.getField(MultipleFields.TEXT);
                holder.setText(R.id.text_single, text);
                break;

            case ItemType.BANNER:
                if (!mIsInitBanner) {
                    itemEntities = item.getField(MultipleFields.ITEMS);
                    bannerImages = new ArrayList<>();
                    for(ItemEntity i: itemEntities){
                        bannerImages.add(i.getCover());
                    }
                    final ConvenientBanner<String> convenientBanner = holder.getView(R.id.banner_recycler_item);
                    BannerCreator.setDefault(convenientBanner, bannerImages, this);
                    mIsInitBanner = true;
                }
                break;

            case ItemType.ITEM:
                itemEntity = item.getField(MultipleFields.ITEM);
                text = itemEntity.getTitle();
                holder.setText(R.id.tv_multiple, text);
                Glide.with(mContext)
                        .load(itemEntity.getCover())
                        .apply(REQUEST_OPTIONS)
                        .into((ImageView) holder.getView(R.id.img_multiple));
                break;

            case ItemType.SERVICE:
                break;

            case ItemType.ITEM_9:
                itemEntities = item.getField(MultipleFields.ITEMS);

                holder.setText(R.id.tv_0, itemEntities.get(0).getTitle());
                holder.setText(R.id.tv_1, itemEntities.get(1).getTitle());
                holder.setText(R.id.tv_2, itemEntities.get(2).getTitle());
                holder.setText(R.id.tv_3, itemEntities.get(3).getTitle());
                holder.setText(R.id.tv_4, itemEntities.get(4).getTitle());
                holder.setText(R.id.tv_5, itemEntities.get(5).getTitle());
                holder.setText(R.id.tv_6, itemEntities.get(6).getTitle());
                holder.setText(R.id.tv_7, itemEntities.get(7).getTitle());
                holder.setText(R.id.tv_8, itemEntities.get(8).getTitle());

                Glide.with(mContext).load(itemEntities.get(0).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_0));
                Glide.with(mContext).load(itemEntities.get(1).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_1));
                Glide.with(mContext).load(itemEntities.get(2).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_2));
                Glide.with(mContext).load(itemEntities.get(3).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_3));
                Glide.with(mContext).load(itemEntities.get(4).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_4));
                Glide.with(mContext).load(itemEntities.get(5).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_5));
                Glide.with(mContext).load(itemEntities.get(6).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_6));
                Glide.with(mContext).load(itemEntities.get(7).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_7));
                Glide.with(mContext).load(itemEntities.get(8).getCover()).apply(REQUEST_OPTIONS).into((ImageView) holder.getView(R.id.iv_8));

                break;

            default:
                break;
        }
    }

    public static MultipleRecyclerAdapter create(List<MultipleItemEntity> data) {
        return new MultipleRecyclerAdapter(data);
    }

    public static MultipleRecyclerAdapter create(DataConverter converter) {
        return new MultipleRecyclerAdapter(converter.convert());
    }

    private void init() {
        //设置不同的Item布局
        addItemType(ItemType.NEWS_WITH_PIC, R.layout.item_news_cnt_list_withpic);
        addItemType(ItemType.NEWS, R.layout.item_news_cnt_list);

        addItemType(ItemType.TEXT, R.layout.item_multiple_text);
        addItemType(ItemType.BANNER, R.layout.item_multipe_banner);
        addItemType(ItemType.SERVICE, R.layout.item_multipe_service);
        addItemType(ItemType.ITEM, R.layout.item_entity);
        addItemType(ItemType.ITEM_9, R.layout.item_multipe_items_group);

        //设置宽度监听
        setSpanSizeLookup(this);
        openLoadAnimation();
        //多次执行动画
        isFirstOnly(false);
    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return getData().get(position).getField(MultipleFields.SPAN_SIZE);
    }


    @Override
    public void onItemClick(int position) {
        Log.e(TAG, "onItemClick: banner@@" + position);
    }

}
